<?php

include "testingsocket.php";

if ($_COOKIE['ck'] != COOKIEKEY) { header("location: login.php"); }

session_start();

if ($_GET['add']) {
	if (!isset($_SESSION['plot'][$_GET['add']])) {
		if(!is_array($_SESSION['plot'])) {
			$_SESSION['plot'] = array();
		}
		if (in_array($_GET['zone'],$_SESSION['plot']) == FALSE) {
			$_SESSION['plot'][$_GET['zone']] = $_GET['add'];
		}
	}
}

if ($_GET['clear']) {
	unset($_SESSION['plot']);
}

if ($_GET['remove']) {
	unset($_SESSION['plot'][$_GET['remove']]);
}

if ($_GET['doit'] == "loadit") {
	//Marian .... opening connection to client backend process and requesting a send to each file stored in the session array.
	//echo "loading ..........";
	//print_r($_SESSION['filesList']);
	foreach($_SESSION['filesList'] as $key=>$value)
    {
		//TODO: change this later to retrieve the connection parameters from a session variable
		//connect("localhost", 55555);
		//put : server path , client path
		echo "<p>sending " .$value. "</p><br/>";
		if(put($_SESSION["remDir"]."/".substr(strrchr($value, "/"), 1),$value) === "DONE")
			echo "Sent successfully";
	}
	//reload the server files again
	echo "<SCRIPT LANGUAGE='javascript'>go(document.getElementById('rem_dir').value);</SCRIPT>";
	
	//clear the list now ... 	
	echo "<SCRIPT LANGUAGE='javascript'>setTimeout(\"clearbasket()\",500);</SCRIPT>";
}


if (is_array($_SESSION['plot'])) {
	$_SESSION['filesList'] = array();
	$jump = serialize($_SESSION['plot']);
	$cellno = 1;
	 $mynumbers = "<table cellpadding=\"1\" cellspacing=\"0\" width=\"100%\"><tr style=\"background-color:#efefef;font-weight:bold;\"><td colspan=\"2\" class=\"graylabel\">Files</td></tr>";
	 foreach ($_SESSION['plot'] as $key=>$val) {
		$string = base64_decode($val);
		//echo $string;
		$len = strlen($string);
		//echo $len;
		$filename = substr($string, 0 , len-1);
		//echo $filename;
		array_push($_SESSION['filesList'], $filename);
		$mycell = "trcella".$cellno;
		$mynumbers .= "<tr id=\"$mycell\" onMouseover=\"if (oncell != '$mycell') {overm('$mycell');}\" onMouseout=\"if (oncell != '$mycell') {outm('$mycell');}\"><td><a href=\"loadfiles.php?x=".$key."&amp;f=".$val."\" title=\"".base64_decode($key)."\">".chopstring(cut_string_using_last($string),FILENAMEMAXLENGTH,'')."</a></td><td style=\"width:5px;\"><a href=\"javascript:remove('$key')\"><img src=\"img/nolines_minus.gif\" alt=\"Remove from my list: ".base64_decode($val)."\" style=\"width:18px;height:18px; border:0px none;\" /></a></td></tr>";
		 $cellno++;
	 }
	 $mynumbers .= "<tr><td colspan=\"2\" style=\"text-align:right\"><a href=\"javascript:clearbasket();\"><img src=\"img/trash.gif\" alt=\"Clear bookmark list\" style=\"width:18px;height:18px; border:0px none; vertical-align:text-bottom; vertical-align: middle;\" /></a> <a href=\"javascript:clearbasket();\">Clear</a>&nbsp;<a href=\"javascript:dozip();\"><img src=\"img/cd.gif\" alt=\"Download list\" style=\"width:19px;height:17px; border:0px none; vertical-align:text-bottom; vertical-align: middle;\" /></a> <a href=\"javascript:dozip();\">Zip</a></td></tr></table>";
} else {
	$mynumbers = "<strong>My file list</strong><p>There are currently no files in your list. Click on the <img src=\"img/nolines_plus.gif\" alt=\"Add to my list\" style=\"width:18px;height:18px; border:0px none; vertical-align:text-bottom; vertical-align: middle;\" /> icon displayed on the file listing view to add a file to your list.</p>";
}

if ($_GET['sendit'] AND is_array($_SESSION['plot']) AND $_GET['doit'] == "saveit") {
	echo "<p><table cellpadding=\"1\" cellspacing=\"0\" width=\"100%\"><tr><td style=\"background-color:#efefef;font-weight:normal;\">The list has been saved using the following email address <strong>".$_GET['sendit']."</strong></td></tr></table></p>";

	storesession($_GET['sendit'],serialize($_SESSION['plot']));

}



?>
<table cellspacing="0" cellpadding="0" style="background-color:#fff; border:0px none; width:100%;">
<tr><td style="border:0px none; padding:0px;">
<?=$mynumbers;?>
</td></tr>
<tr><td style="border:0px none; padding:0px;">
&nbsp;
</td></tr>
<tr><td style="border:0px none; text-align:center; border-top:1px solid #bbb; background-color:#ccc;">
<strong>PUT <input type="radio" value="loadit" id="dowhat" name="dowhat" onclick = "rad_val = 'loadit'" /> OR <input type="radio" value="saveit" id="dowhat" name="dowhat" onclick = "rad_val = 'saveit'" /> GET</strong><br /><br /><br />
<input type="button" id="send" name="send" value="PUT/GET List" onclick="sendlist();" class="buttons" /><p>Select an option (load or save), then click the button</p>
</td></tr>
</table>
<input type="hidden" name="xkzip" id="xkzip" value="<?=base64_encode($jump);?>">